﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MESPro.Models;
using SqlSugar;

namespace MESPro.DBManager
{
    public class ModelManager
    {
        #region 曾
        /// <summary>
        /// 添加一个模型,不包括子类(add=httppost)
        /// </summary>
        /// <returns>返回插入成功的行数</returns>
        public int AddModel(Model Model)
        {
            var res = DbContext.Db.Insertable(Model).ExecuteCommand();
            return res;
        }
        /// <summary>
        /// 添加一个模块,包括子类(add=httppost)
        /// </summary>
        /// <returns>返回插入成功的行数</returns>
        public bool AddModelInfo(Model FunctionGroup)
        {
            var res = DbContext.Db.InsertNav(FunctionGroup)
                .Include(x => x.FunctionGroups, new InsertNavOptions() { OneToManyIfExistsNoInsert = true })
                .ExecuteCommand();
            return res;
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除一个模型(导航删除)和他的子页面(Delete=HttpDelete)
        /// </summary>
        /// <returns>返回是否删除成功</returns>
        public bool DeleteModel(Model Model)
        {
            List<Model> list = new List<Model> { Model };
            var res = DbContext.Db
                .DeleteNav<Model>(a => a.Id == Model.Id)
                .Include(a => a.FunctionGroups)
                .ExecuteCommand();
            return res;
        }
        #endregion

        #region 改
        /// <summary>
        /// 修改一个页面和他的子页面(Update==httpput)
        /// 先更新主表然后,然后删除子表,在然后插入子表
        /// </summary>
        /// <returns></returns>
        public bool UpdateModelInfo(Model Model)
        {
            List<Model> list = new List<Model>() { Model };
            var res = DbContext.Db
                .UpdateNav(list)
                .Include(z1 => z1.FunctionGroups)
                .ExecuteCommand();
            return res;
        }
        #endregion

        #region 查
        /// <summary>
        /// 查询所有的页面,以及子类
        /// </summary>
        /// <returns></returns>
        public List<Model> GetModelListInfo()
        {
            var res = DbContext.Db
                .Queryable<Model>()
                .Includes(
                a => a.FunctionGroups,
                b => b.Functions,
                c => c.DbControlGs)
                .ToList();
            return res;
        }
        /// <summary>
        /// 查询所有的页面,以及子类
        /// </summary>
        /// <returns></returns>
        public List<Model> GetModelListInfo(User user, Model model)
        {
            var res = DbContext.Db
                .Queryable<Model>()
                .Where(x=>x.DisName==model.DisName)
                .Includes(
                a => a.FunctionGroups,
                b => b.Functions,
                c => c.DbControlGs)
                .ToList();
            return res;
        }
        /// <summary>
        /// 查询所有的页面,不包括子类
        /// </summary>
        /// <returns></returns>
        public List<Model> GetModelList()
        {
            var res = DbContext.Db.Queryable<Model>().ToList();
            return res;
        }
        #endregion

    }
}
